package com.kang.delay;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import javax.xml.stream.FactoryConfigurationError;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangkangkang
 * @Description: 延迟插件
 * @date 2021/8/3 15:02
 */
public class Test4 {
    static final String exchangeName = "test_exchange";
    static final String queueName = "test_queue";
    static final String routingKey = "test_queue";

    public static void main(String[] args) throws IOException, InterruptedException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        Map map = new HashMap();
        map.put("x-delayed-type", "direct");
        //创建路由
        channel.exchangeDeclare(exchangeName, "x-delayed-message", false, false, map);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        //发送
        String msg = "延迟消息";
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        Map headers = new HashMap();
        headers.put("x-delay", 9000);
        builder.headers(headers);
        AMQP.BasicProperties build = builder.build();
        channel.basicPublish(exchangeName, queueName, build, msg.getBytes());
        RabbitMQUtils.closeConnectionAndChanel(channel, connection);
    }
}
